#include "user_queue.h"
#include <stdlib.h>
#include <string.h>

// 初始化队列
bool queue_init(Queue_t *queue, size_t capacity) {
    if (!queue || capacity == 0) return false;

    queue->data = (void **)malloc(capacity * sizeof(void *));
    if (!queue->data) return false;

    queue->capacity = capacity;
    queue->size = 0;
    queue->front = 0;
    queue->rear = 0;

    return true;
}

// 销毁队列
void queue_destroy(Queue_t *queue) {
    if (!queue || !queue->data) return;

    free(queue->data);
    queue->data = NULL;
    queue->capacity = 0;
    queue->size = 0;
    queue->front = 0;
    queue->rear = 0;
}

// 入队
bool queue_enqueue(Queue_t *queue, void *item) {
    if (!queue || queue_is_full(queue)) return false;

    queue->data[queue->rear] = item;
    queue->rear = (queue->rear + 1) % queue->capacity;
    queue->size++;

    return true;
}

// 出队
void *queue_dequeue(Queue_t *queue) {
    if (!queue || queue_is_empty(queue)) return NULL;

    void *item = queue->data[queue->front];
    queue->front = (queue->front + 1) % queue->capacity;
    queue->size--;

    return item;
}

// 获取队列当前大小
size_t queue_size(Queue_t *queue) {
    return queue ? queue->size : 0;
}

// 判断队列是否为空
bool queue_is_empty(Queue_t *queue) {
    return queue ? queue->size == 0 : true;
}

// 判断队列是否已满
bool queue_is_full(Queue_t *queue) {
    return queue ? queue->size == queue->capacity : false;
}
